package com.kungkhies.services.kungkher;

import static java.lang.Math.*;
import static java.util.Collections.*;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.kungkhies.domain.kungkher.Kungkher;
import com.kungkhies.domain.kungkhie.KungkhieSummary;
import com.kungkhies.persistence.kungkher.KungkherDao;

@Service("kungkherService")
@Transactional(propagation = Propagation.REQUIRED)
public class KungkherServiceImpl implements KungkherService
{

//    public KungkherServiceImpl(){}
  
    public void saveKungkhie(KungkhieSummary kungkhie)
    {
        kungkhie.setWhen(new Date());
        kungkherDao.saveKungkhie(kungkhie);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public List<KungkhieSummary> getRecentKungkhies(int count)
    {
        List<KungkhieSummary> recentKungkhies = kungkherDao.getRecentKungkhies();

        //reverse(recentKungkhies);

        return recentKungkhies;
    }

    public void saveKungkher(Kungkher kungkher)
    {
        if (kungkher.getId() == null)
        {
            kungkherDao.addKungkher(kungkher);
        }
        else
        {
            kungkherDao.saveKungkher(kungkher);
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Kungkher getKungkher(long id)
    {
        return kungkherDao.getKungkherById(id);
    }

    public void startFollowing(Kungkher follower, Kungkher followee)
    {
        // TODO Auto-generated method stub  
    }

    public List<KungkhieSummary> getKungkhiesForKungkher(Kungkher kungkher)
    {
        return kungkherDao.getKungkhiesForKungkher(kungkher);
    }

    public List<KungkhieSummary> getKungkhiesForKungkher(String username)
    {
        Kungkher kungkher = kungkherDao.getKungkherByUsername(username);
        return getKungkhiesForKungkher(kungkher);
    }

    public Kungkher getKungkher(String username)
    {
        return kungkherDao.getKungkherByUsername(username);
    }

    public void deleteKungkhie(long id)
    {
        kungkherDao.deleteKungkhie(id);
    }

    public List<Kungkher> getAllKungkhers()
    {
        if (kungkherDao == null)
        {
            System.out.println("kungkherDao is NULL in KungkherServiceImpl");
        }
        else
        {
            System.out.println("kungkherDao is NOT NULL in KungkherServiceImpl");
        }
        return kungkherDao.findAllKungkhers();
    }
    
    
    private KungkherDao kungkherDao;
    @Autowired
    public void setKungkherDao(KungkherDao kungkherDao)
    {
        this.kungkherDao = kungkherDao;
    }

    public KungkhieSummary getKungkhieById(long id)
    {
        return kungkherDao.getKungkhieById(id);
    }

    @Override
    public boolean kungkherNameExists(String kungkherName)
    {
        if (kungkherDao == null)
        {
            System.out.println("kungkherDao is NULL in KungkherServiceImpl");
        }
        else 
        {
            System.out.println("kungkherDao is NOT NULL in KungkherServiceImpl");
        }
        return kungkherDao.kungkherNameExists(kungkherName);
    }

    @Override
    public boolean emailAddressExists(String emailAddress)
    {
        return kungkherDao.emailAddressExists(emailAddress);
    }
}